package it.unipg.bipod.web;

import java.text.Format;
import java.text.SimpleDateFormat;

import it.unipg.bipod.applicationLogic.*;
import it.unipg.bipod.dataModel.*;

import javax.faces.bean.*;

@ManagedBean(name="segretarioDettaglioBando")
@ViewScoped
public class SegretarioDettaglioBandoBean extends DefaultBean {
	@ManagedProperty(value="#{segretario.application}")
	private SegretarioApplication application;
	private int idBando;
	private Bando bando;
	
	public SegretarioDettaglioBandoBean() {
		this.application = null;
		this.bando = null;
		this.idBando = -1;
	}
	
	public void setApplication(SegretarioApplication application) {
		this.application = application;
	}

	public SegretarioApplication getApplication() {
		return application;
	}
	
	public String getDataPubblicazione() {
		Format dateFormat = new SimpleDateFormat("dd/MM/yyyy");
		return dateFormat.format(bando.getDataPubblicazione());
	}
	
	public String getDataScadenza() {
		Format dateFormat = new SimpleDateFormat("dd/MM/yyyy");
		return dateFormat.format(bando.getDataScadenza());
	}

	public void setIdBando(int idBando) {
		try {
			bando = application.getBando(idBando);
			
			if (bando == null) {
				// Bando non trovato
				addWarnMessageFor(getSettings().getProperty("msgErroreBandoNonTrovato"), "idBando");
				return;
			}
			
			this.idBando = idBando;
		} catch (ApplicationException e) {
			addWarnMessageFor(getSettings().getProperty("msgErroreBandoNonTrovato"), "idBando");
		}
	}

	public int getIdBando() {
		return idBando;
	}
}
